package com.hznu.jspFinal.controller;


import com.hznu.jspFinal.entity.Course;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(description = "课程管理接口",value = "course")

@RestController
@RequestMapping("/Course")
public class CourseController {


    @Autowired
    private CourseService courseService;

    /**
     * author:wp
     * time:12:00 2018/6/3
     *
     * function:新增课程
     * @param course
     * @return
     */

    @ApiOperation(value = "新增课程")
    @PostMapping("/add")
    public R addOne(@RequestBody Course course)
    {
        try{
            if(courseService.findByName(course.getName())==null){
                courseService.addOne(course);
                return R.ok();
            }
            return R.error("该课程已存在");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author:wp
     * time:12:00 2018/6/3
     *
     * function:通过id查找课程
     * @param id
     * @return
     */

    @ApiOperation(value = "通过id查找课程信息")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",courseService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author:wp
     * time:12:00 2018/6/3
     *
     * function:修改课程信息
     * @param course
     * @return
     */
    @ApiOperation(value = "修改课程信息")
    @PostMapping("/update")
    public R update(@RequestBody Course course)
    {
        try{
            if(courseService.findByName(course.getName())==null) {
                courseService.update(course);
                return R.ok();
            }
            return R.error("该课程已存在");
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author:wp
     * time:12:00 2018/6/3
     *
     * function:删除课程
     * @param id
     * @return
     */
    @ApiOperation(value = "删除课程")
    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable Integer id)
    {
        try{
            courseService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * author:wp
     * time:12:00 2018/6/3
     *
     * function:查找一个教师的所有课程
     * @param creatorid
     * @return
     */
    @ApiOperation(value = "查找一个教师的所有课程")
    @GetMapping("/findByCreatorid/{creatorid}")
    public R findByCreatorid(@PathVariable Integer creatorid)
    {
        try{
            return R.ok().put("data",courseService.findByCreatorid(creatorid));
        }
        catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

}
